﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDom;
using System.Web.Mvc.Html;
using Bzure.Apps.Security.ViewModels;

namespace Bzure.Apps.Security.Views
{
    public class ResetPassword : PageView<ResetPasswordViewModel>
    {
        public virtual ContainerTag TitleContent(ContainerTag container)
        {
            return container[Model.Title];
        }
        string Script1()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" $().ready(function () {");
            sb.Append(" $(\"#ResetPasswordForm\").validate({");
            sb.Append("rules: {NewPassword: {required: true, minlength: 5},");
            sb.Append("ConfirmPassword: {required: true, minlength: 5,equalTo: \"#NewPassword\"}},");
            sb.Append("messages: {NewPassword: {required: \"请输入密码\", minlength: \"密码至少需要5个字符\"},");
            sb.Append("ConfirmPassword: {required: \"请确认密码\", minlength: \"密码至少需要5个字符\", equalTo: \"两次输入密码不一致\"}}");
            sb.Append("  });});");
            return sb.ToString();
        }
        string Script2(string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" var url = \"{0}\";",url);
            sb.Append("  var ResetPassword = function () {");
            sb.Append(" var form = $(\"#ResetPasswordForm\");");
            sb.Append("if (form.valid()) {");
            sb.Append(" var serializedForm = form.serialize();");
            sb.Append("$.post(url, serializedForm, function (result) {");
            sb.Append("  $(\"#content_box_messages\").html(result);");
            sb.Append(" $(\".close\").click(");
            sb.Append("	function () {");
            sb.Append(" $(this).parent().fadeTo(400, 0, function () { ");
            sb.Append(" $(this).slideUp(400);");
            sb.Append("  }); return false;});");
            sb.Append(" });    }  };");
            return sb.ToString();
        }
        public virtual ContainerTag MainContent(ContainerTag container)
        {
            return container
            [
                  script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery.validate.min.js" })),
                script.attr(type: MimeType.text.javascript)[Script2(Model.Url)],
                script.attr(type: MimeType.text.javascript)[Script1()],
                h2[Model.Title],
                p.attr(id: "page-intro")["请谨慎使用重置用户密码功能，重置后原密码将失效和无法找回。"],
                div.attr(@class: "clear"),
                div.attr(@class: "content-box")
                [
                     div.attr(@class: "content-box-header")
                      [
                            h3["用户密码"],
                            ul.attr(@class: "content-box-tabs")
                            [
                               li[a.attr(href: "#tab1", @class: "default-tab")["重置"]]
                            ],
                            div.attr(@class: "clear")
                      ],
                     div.attr(@class: "content-box-content")
                      [
                          div.attr(@class: "tab-content default-tab", id: "tab1")
                          [
                                div.attr(id:"content_box_messages"),
                                form.attr(id: "ResetPasswordForm")
                                [
                                       p.attr(id: "ResetPassword_UserName")
                                        [
                                             label.attr(@for: "RoleName")["用&nbsp;户&nbsp;名：&nbsp;"+Model.UserName ]
                                           
                                        ],
                                        p.attr(id: "ResetPassword-New")
                                        [
                                             label.attr(@for: "NewPassword")["密&nbsp;&nbsp;码："],
                                             input.attr(type: InputType.password, id: "NewPassword", name: "NewPassword").ext("autocomplete","off")
                                        ],
                                         p.attr(id: "ResetPassword-ConfirmPassword")
                                        [
                                             label.attr(@for: "ConfirmPassword")["确认密码："],
                                             input.attr(type: InputType.password, id: "ConfirmPassword", name: "ConfirmPassword").ext("autocomplete", "off")
                                        ],
                                         p.attr(id: "ResetPassword-submit")
                                        [
                                             input.attr(type: InputType.button, @class: "button", value: "重置", onclick: "ResetPassword();")
                                        ],
                                        p.attr(id: "ResetPassword_ReturnUrl")
                                        [
                                             a.attr(href: Url.RouteUrl("SecurityApp_User", new { action = "UserList" }))["返回用户列表"]
                                        ]
                                ]
                          ]
                      ]
                ]
            ];
        }

        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return ReusableCreateTagTree(TitleContent, MainContent, Model.MasterViewModel);
        }
    }
}
